/**
 * 项目名称:
 *
 * @Author: 夏国倩
 * @Date: 2023/10/17 10:15
 * @Copyright
 */
package itSystemCase7.dao;
import itSystemCase7.domain.QQBean;
import itSystemCase7.tools.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class QQDao {
    public List<QQBean> showUser() {

        /**
         * 显示用户信息
         */
        List<QQBean> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "select * from userinfo";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("UserId");
                String userName = rs.getString("UserName");
                String userPwd = rs.getString("UserPwd");
                int levelId = rs.getInt("LevelId");
                String email = rs.getString("Email");
                double onLineDay = rs.getDouble("OnLineDay");

                //将结果集的每行数据存进对象list中
                QQBean z = new QQBean();
                z.setUserId(id);
                z.setUserName(userName);
                z.setUserPwd(userPwd);
                z.setLevelId(levelId);
                z.setEmail(email);
                z.setOnLineDay(onLineDay);

                //将对象封装到List中
                list.add(z);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            DBUtil.close(conn, ps, rs);
        }
        return null;
    }

        /**
         * 更新在线天数
         * @param id
         */
        //调用update去更新数据库中的userinfo
        public void updateOnLineDays ( int id){
            Scanner sc = new Scanner(System.in);
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            String sql = null;
            int count = 0;
            try {
                conn = DBUtil.getConnection();
                //更新在线天数
                sql = "update userinfo set  OnLineDay= ? where UserId = " + id;
                ps = conn.prepareStatement(sql);
                while (true) {
                    System.out.print("请输入新的在线天数：");
                    int day = sc.nextInt();
                    if (day > 0) {
                        ps.setDouble(1, day);
                        break;
                    } else {
                        System.out.println("您输入的天数不正确，请重新输入");
                    }
                }
                count = ps.executeUpdate();
                System.out.println("受影响的条数：" + count);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(conn, ps, rs);
            }
        }

        /**
         * 删除用户
         * @param id
         */
        public void deleteUser ( int id){
            Connection conn = null;
            PreparedStatement ps = null;
            try {
                conn = DBUtil.getConnection();
                String sql = "delete from userinfo where UserId=?";
                ps = conn.prepareStatement(sql);
                ps.setInt(1, id);
                ps.executeUpdate();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(conn, ps, null);
            }
        }

        /**
         * 添加用户
         * @param qq
         */
        public void addUser (QQBean qq){
            Connection conn = null;
            PreparedStatement ps = null;
            try {
                conn = DBUtil.getConnection();
                String sql = "insert into userinfo(UserName,UserPwd,LevelId,Email,OnLineDay) values(?,?,?,?,?)";
                ps = conn.prepareStatement(sql);
                ps.setString(1, qq.getUserName());
                ps.setString(2, qq.getUserPwd());
                ps.setInt(3, qq.getLevelId());
                ps.setString(4, qq.getEmail());
                ps.setDouble(5, qq.getOnLineDay());

                int count = ps.executeUpdate();
                System.out.println("受影响的条数：" + count);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(conn, ps, null);
            }
        }
    }
